//
//  KUIHeadPageSwithView.m
//  Youbar
//
//  Created by  Lead To Asia 2 on 10/16/12.
//  Copyright (c) 2012 YaoXiaoxi. All rights reserved.
//

#import "HeadPageSwithView.h"
//#import "WPDebug.h"



@interface HeadPageSwithView()

/*array of button*/
@property (strong, nonatomic) NSMutableArray *		m_arrayButton;
//@property (strong, nonatomic) UIImageView*          ui_selectedImage;
@property (assign, nonatomic) NSInteger            m_intWantGoToPage;

/*array of ImageView */
//@property (strong, nonatomic) NSMutableArray *						m_arrayImageView;




///*array of 外面告诉按钮的名字是string*/
//@property (strong, nonatomic) NSMutableArray*						m_arrayData; 

@end

@implementation HeadPageSwithView

- (id)init
{
    self = [super init];
    if(self)
	{
        
        
    }
    
    return self;
}

-(id)initWithFrame:(CGRect) frame {
    self = [super initWithFrame:frame];
    if(self)
	{
       
    }
    return self;
}

-(void)awakeFromNib
{
    [self initButtonToArray];
}

/**************************************************************************************/

#pragma mark -
#pragma mark Input Parameters //这是必填项
#pragma mark -

/**************************************************************************************/


-(void)initButtonToArray
{

    _m_arrayButton = [[NSMutableArray alloc]init];
    for (UIView *view in self.subviews) {
        if ([view isMemberOfClass:[UIButton class]]) {
            [_m_arrayButton addObject:view];
        }
    }
    
     self.m_intWantGoToPage = 0;
    [self swithTo:[self.m_arrayButton objectAtIndex:0]
         switched:^(int _index) {}];
}


// ScrollView滚动 调用

-(void)setHeaderPageChangGoToButtonIndex:(NSInteger)_buttonIndex
{
//	//告诉内部端由手游页游切换动画
//	
	if (_buttonIndex <0 || _buttonIndex >[self.m_arrayButton count]-1|| _buttonIndex ==self.m_intWantGoToPage)
	{
		return;
	}
    self.m_intWantGoToPage = _buttonIndex;
	[self swithTo:[self.m_arrayButton objectAtIndex:_buttonIndex]
         switched:^(int _index) {}];
}

/**************************************************************************************/

#pragma mark -
#pragma mark internal member functon
#pragma mark -

/**************************************************************************************/


//几个button之间的转换

typedef void (^KUISwitchHeadPage_Swith)(int _index);

- (void)swithTo:(UIButton *)_buttonSender
        switched:(KUISwitchHeadPage_Swith)_blockSwitch
{
    
    for (UIButton *button in self.m_arrayButton )
    {
        [button setBackgroundColor:[UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0]];
    }
    
    [_buttonSender setBackgroundColor:[UIColor colorWithRed:140.0/255.0 green:10.0/255.0 blue:65.0/255.0 alpha:1.0]];

}

/**************************************************************************************/

#pragma mark -
#pragma mark Touch Tap
#pragma mark -

/**************************************************************************************/

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch	=	[touches anyObject];
	CGPoint point	=	[touch locationInView:self];
	for (UIButton *button in self.m_arrayButton)
	{
        //temp add button edge
        CGRect rect = CGRectMake(button.frame.origin.x, button.frame.origin.y-10, button.frame.size.width, button.frame.size.height+10*2);
		if (CGRectContainsPoint(rect, point))
		{
			//告诉外界scroll滚动
			NSInteger _index	=	[self.m_arrayButton indexOfObject:button];
			 [self.m_delegate delegateOnHeadPageSwitchButtonTap:_index];
		}
	}

}

@end
